package com.kish.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kish.pojo.User;
import com.kish.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

@Controller
public class UserController {
    @Resource
    private UserService userService;

    @RequestMapping("/user/index")
    public String index() {
        userService.save();
        System.out.println("user index");
        return "/index";
    }

//    @RequestMapping("/user/add")
//    public ModelAndView add() {
//        ModelAndView modelAndView = new ModelAndView();
//        modelAndView.setViewName("/add");
//        return modelAndView;
//    }

    @RequestMapping("/user/update")
    public String update(HttpServletRequest request) {
        request.setAttribute("username", "张三");
        return "/views/user/update";
    }


    @RequestMapping("/user/add")
    public ModelAndView add() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/views/user/add");
        modelAndView.addObject("username", "李四");
        modelAndView.addObject("age", "18");
        return modelAndView;
    }

//    @RequestMapping("/login")
//    public void login(HttpServletResponse response) throws IOException {
//      response.getWriter().write("登录成功");
//    }

    @RequestMapping("/login")
    @ResponseBody //返回值不是页面而是字符串
    public String login() throws IOException {
        return "登录成功";
    }


    @RequestMapping(value = "/user/one", produces = {"text/html;charset=UTF-8;", "application/json;"})
    @ResponseBody //返回值不是页面而是字符串
    public String getOneUser(HttpServletResponse response) throws JsonProcessingException {
        User user = new User("张三", 18, "男");
        System.out.println(user);
        response.setContentType("application/json;charset=UTF-8");
        //{"user":"zhangsan","age":18,"gender":"nan"}
        // return "{\"name\":\"" + user.getName() + "\" ,\"age\":\"" + user.getAge() + "\" }";
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(user);
    }

    @RequestMapping("/user/all")
    @ResponseBody //返回值不是页面而是字符串
    public String getUsers() throws JsonProcessingException {
        User zs = new User("zhangSan", 18, "man");
        User ls = new User("leesi", 18, "man");
        User ww = new User("wangwu", 18, "man");
        ArrayList<User> users = new ArrayList<>();
        users.add(zs);
        users.add(ls);
        users.add(ww);
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(users);
    }

    @RequestMapping("/user/query")
    @ResponseBody //返回值不是页面而是字符串
    public User getUserById() {
        User zs = new User("zhangSan", 18, "man");
        return zs;
    }

    @RequestMapping("/user/alls")
    @ResponseBody //返回值不是页面而是字符串
    public ArrayList<User> getAllUsers() throws JsonProcessingException {
        User zs = new User("zhangSan", 18, "man");
        User ls = new User("leesi", 18, "man");
        User ww = new User("wangwu", 18, "man");
        ArrayList<User> users = new ArrayList<>();
        users.add(zs);
        users.add(ls);
        users.add(ww);
        return users;
    }
}
